<?php
namespace app\home\controller;
use app\home\controller\Commen;
use app\home\model\File;
use app\home\model\Users;
use app\home\model\Score;

class Index extends Commen
{
    public function __construct(){
        parent::__construct();
    }
    public function index()
    {
        $File = new File;
//        halt($File);
        $data = File::all();
//        halt($data);
        $file_result = $File->order('id desc')->paginate();
        $page = $file_result->render();
//        halt($file_result);
        $file_del = File::onlyTrashed()->paginate();
        $file_del_page = $file_result->render();

        $data = Users::get(1);
//        halt($data);

        $this->assign('file_del', $file_del);
        $this->assign('file_del_page', $file_del_page);

        $this->assign('file_result', $file_result);
        $this->assign('page', $page);
        return $this->fetch();
    }

    public function fileUpload()
    {
        if(request()->isPost())
        {
            $file = request()->file('file');
            if($file)
            {
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 20160820/42a79759f284b767dfcb2a0197904287.jpg
                    $data['file'] =  $info->getSaveName();
                    $bool = db('file')->insert($data);

                }else{
                    // 上传失败获取错误信息
                    echo $file->getError();
                }
                if($bool){
                    $this->success('yes');
                }else{
                    $this->error('no');
                }
            }
        }
    }

    public function deleteFile()
    {
        $id = input('id');
        if($id){
//            $data = db('file')->where('id='.$id)->find();
//            $file = File::get($id);
//            $data = json_encode($data);
//            $file = ROOT_PATH . 'public/uploads/'.$data['file'];
//            $file_dir = dirname($file);
//            if(is_file($file)){
//                unlink($file);  //删除文件
//                if(is_dir($file_dir)){
//                    @rmdir($file_dir);  //删除空目录
//                }
//            }
//            $bool = db('file')->where('id='.$id)->delete();
//            $this->success($data);
            $bool = File::destroy($id);//软删除
//            $this->success($bool);
        }

        if($bool)$this->success('yes', url('index/index'));
        $this->error('no', url('index/index'));

    }

    public function renew()
    {
        $id = input('id');
        if($id){
            $file = File::onlyTrashed()->find($id);
            $bool = $file->restore();
        }
        if($bool)$this->success('yes', url('index/index'));
        $this->error('no', url('index/index'));
    }

    public function upload(){

    }

}
